package com.yyq.common;

import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author yyq
 * @create 2021-08-03 21:10
 */
public class test {
    @Test
    public void Test(){
        Good arr[]=new Good[4];

        arr[0]=new Good("xiaomi",12);
        arr[1]=new Good("huawei",62);
        arr[2]=new Good("apple",112);
        arr[3]=new Good("leven",72);
        //对象排序 根据价格从低到高
       Arrays.sort(arr, new Comparator<Good>() {
           @Override
           public int compare(Good o1, Good o2) {
               Good g1=o1;
               Good g2=o2;
               if(g1.getPrice()>g2.getPrice()){
                   return 1;
               }
               else if(g1.getPrice()<g2.getPrice())
                   return -1;
               else return 0;

           }
       });
        for (Good good : arr) {
            System.out.println(good.getName());
        }
    }
}
